using System;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Dimok;
using Dimok.Exceptions;
using Dimok.User;

namespace Dimok.Images
{
	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Post pdf. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class PostPdf : System.Web.UI.Page{
		protected System.Web.UI.HtmlControls.HtmlAnchor linkSavedFile;
		private UserObject uox = new UserObject();

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Event handler. Called by Page for load events. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="sender">	Source of the event. </param>
		/// <param name="e">			Event information. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		private void Page_Load(object sender, System.EventArgs e)
		{
				Stream stream = Request.InputStream;
				if(stream.Length == 0) throw new ClientException("No pdf input");
				byte[] pdfIn = new byte[stream.Length];
				stream.Read(pdfIn,0,(int)stream.Length);
				byte[][] pages = Dimok.ABCPdf.Slice(pdfIn);
				byte[][] pagesIn = new byte[pages.Length-1][];
				for(int i=0;i<pages.Length-1;i++) pagesIn[i] = pages[i];
				int folderID = ShowApplImageFolder.ImageFolderIDParam[0];
				//int pageStart = ShowApplImageFolder.ImagePageStartParam;
				int pageCount = 0;//ShowApplImageFolder.ImagePageCountParam;
				int existPagesCount = Pages.ImagePageCount(uox.db_Images,uox.applID,folderID);
				int updateCount = pageCount <= existPagesCount ? pageCount : existPagesCount;
				int j = 1;
				// Update existing pages
				foreach( byte[] page in pagesIn )
					if( j <= existPagesCount )
						Pages.ImagePageUpdate(uox.db_Images,uox.applID,folderID,PageTypes.PDF,j++,page);
					else
						Pages.ImagePagesInsert(uox.db_Images,uox.applID,folderID,page,j++);
				// Delete
				if( existPagesCount >= j )
					Pages.ImagePagesDelete(uox.db_Images,uox.applID,folderID,-j);

			int newPagesCount = Pages.ImagePageCount(uox.db_Images,uox.applID,folderID);
			linkSavedFile.HRef = "/manager/common/showapplimagefolder.aspx?ApplID="+uox.applID+"&FolderID="+folderID+"&PageCount="+newPagesCount;
		}

		#region Web Form Designer generated code

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Raises the initialise event. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="e">	Event information to send to registered event handlers. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Initializes the component. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}